from threading import Thread

from simplejson import OrderedDict

from app.services.business.gaolu_diagnose import gaolu_diagnose_service
from app.services.chart.Line import Line
from app.utils import date_util, simple_util, string_util
from datetime import timedelta
from app.exts import mysql_pool

import json

from app.utils.thread import CountDownLatch
import logging


class ExpertScore(Line):
    """
    炉渣数据表
    """

    def generate(self, start, end):
        start, end = date_util.parse_start_end_time(start, end, hour_interval=30 * 24)

        last = None
        gaolu_diagnose_records: list = gaolu_diagnose_service.get_records(start, end)

        return dict(columns=
            [{"title": "时间", "dataIndex": "date_time", "key": "date_time"},
             {"title": "产量", "dataIndex": "CG_LT_GL_GL04_RChanliang", "key": "CG_LT_GL_GL04_RChanliang"},
             {"title": "燃料比", "dataIndex": "CG_LT_GL_GL04_RRLB", "key": "CG_LT_GL_GL04_RRLB"},
             {"title": "煤比", "dataIndex": "CG_LT_GL_GL04_Rcokerate", "key": "CG_LT_GL_GL04_Rcokerate"},
             {"title": "全焦负荷", "dataIndex": "CG_LT_GL_GL04_Rquanjiaofuhe", "key": "CG_LT_GL_GL04_Rquanjiaofuhe"},
             {"title": "风量", "dataIndex": "CG_LT_GL_GL04_RLFLL", "key": "CG_LT_GL_GL04_RLFLL"},
             {"title": "富氧", "dataIndex": "CG_LT_GL_GL04_RFYLL", "key": "CG_LT_GL_GL04_RFYLL"},
             {"title": "煤气利用率", "dataIndex": "CG_LT_GL_GL04_RMQLYL", "key": "CG_LT_GL_GL04_RMQLYL"},
             {"title": "鼓风动能", "dataIndex": "CG_LT_GL_GL04_RGFDNKG", "key": "CG_LT_GL_GL04_RGFDNKG"},
             {"title": "热风温度", "dataIndex": "CG_LT_GL_GL04_RRFWD", "key": "CG_LT_GL_GL04_RRFWD"},
             {"title": "炉顶温度", "dataIndex": "CG_LT_GL_GL04_RLDWD", "key": "CG_LT_GL_GL04_RLDWD"},
             {"title": "透气性指数", "dataIndex": "CG_LT_GL_GL04_RTQXZS", "key": "CG_LT_GL_GL04_RTQXZS"},
             {"title": "下部压差占比", "dataIndex": "CG_LT_GL_GL04_RXBYCZB", "key": "CG_LT_GL_GL04_RXBYCZB"},
             {"title": "热负荷", "dataIndex": "CG_LT_GL_GL04_RRFH", "key": "CG_LT_GL_GL04_RRFH"},
             {"title": "崩、坐料、管道次数", "dataIndex": "CG_LT_GL_GL04_RbengzuoguanCi",
              "key": "CG_LT_GL_GL04_RbengzuoguanCi"},
             {"title": "阀座温度", "dataIndex": "CG_LT_GL_GL04_RFZWD", "key": "CG_LT_GL_GL04_RFZWD"},
             {"title": "日出铁次数", "dataIndex": "CG_LT_GL_GL04_RchutieCi", "key": "CG_LT_GL_GL04_RchutieCi"},
             {"title": "两场铁量偏差", "dataIndex": "CG_LT_GL_GL04_Rtiecha", "key": "CG_LT_GL_GL04_Rtiecha"},
             {"title": "物理热", "dataIndex": "CG_LT_GL_GL04_Rtswd", "key": "CG_LT_GL_GL04_Rtswd"},
             {"title": "Mg/Al", "dataIndex": "CG_LT_GL_GL04_RMg_Al", "key": "CG_LT_GL_GL04_RMg_Al"},
             {"title": "[Si+Ti]", "dataIndex": "CG_LT_GL_GL04_RSi_Ti", "key": "CG_LT_GL_GL04_RSi_Ti"},
             {"title": "炉渣R2", "dataIndex": "CG_LT_GL_GL04_RR2", "key": "CG_LT_GL_GL04_RR2"},
             {"title": "水温差", "dataIndex": "CG_LT_GL_GL04_Rshuiwencha", "key": "CG_LT_GL_GL04_Rshuiwencha"},
             {"title": "温度34m", "dataIndex": "CG_LT_GL_GL04_R34m", "key": "CG_LT_GL_GL04_R34m"},
             {"title": "温度26m", "dataIndex": "CG_LT_GL_GL04_R26m", "key": "CG_LT_GL_GL04_R26m"},
             {"title": "温度21m", "dataIndex": "CG_LT_GL_GL04_R21m", "key": "CG_LT_GL_GL04_R21m"},
             {"title": "炉底中心温度", "dataIndex": "CG_LT_GL_GL04_Rluxin2", "key": "CG_LT_GL_GL04_Rluxin2"},
             {"title": "指标检查", "dataIndex": "zb", "key": "zb"}, {"title": " 操作参数", "dataIndex": "cz", "key": "cz"},
             {"title": "渣铁处理", "dataIndex": "zt", "key": "zt"},
             {"title": "炉缸炉体监控", "dataIndex": "ltlg", "key": "ltlg"}, {"title": " 总分", "dataIndex": "zf", "key": "zf"}]
        , data=gaolu_diagnose_records,
            input=last)

    def download(self, start, end):
        import pandas as pd
        ordered = OrderedDict()
        results = []
        keys = ["date_time", "CG_LT_GL_GL04_RChanliang", "CG_LT_GL_GL04_RRLB", "CG_LT_GL_GL04_Rcokerate", "CG_LT_GL_GL04_Rquanjiaofuhe", "CG_LT_GL_GL04_RLFLL", "CG_LT_GL_GL04_RFYLL", "CG_LT_GL_GL04_RMQLYL", "CG_LT_GL_GL04_RGFDNKG", "CG_LT_GL_GL04_RRFWD", "CG_LT_GL_GL04_RLDWD", "CG_LT_GL_GL04_RTQXZS", "CG_LT_GL_GL04_RXBYCZB", "CG_LT_GL_GL04_RRFH", "CG_LT_GL_GL04_RbengzuoguanCi", "CG_LT_GL_GL04_RFZWD", "CG_LT_GL_GL04_RchutieCi", "CG_LT_GL_GL04_Rtiecha", "CG_LT_GL_GL04_Rtswd", "CG_LT_GL_GL04_RMg_Al", "CG_LT_GL_GL04_RSi_Ti", "CG_LT_GL_GL04_RR2", "CG_LT_GL_GL04_Rshuiwencha", "CG_LT_GL_GL04_R34m", "CG_LT_GL_GL04_R26m", "CG_LT_GL_GL04_R21m", "CG_LT_GL_GL04_Rluxin2", "zb", "cz", "zt", "ltlg", "zf"]
        columns = ["时间", "产量", "燃料比", "煤比", "全焦负荷", "风量", "富氧", "煤气利用率", "鼓风动能", "热风温度", "炉顶温度", "透气性指数", "下部压差占比",
                   "热负荷", "崩、坐料、管道次数", "阀座温度", "日出铁次数", "两场铁量偏差", "物理热", "Mg/Al", "[Si+Ti]", "炉渣R2", "水温差", "温度34m",
                   "温度26m", "温度21m", "炉底中心温度", "指标检查", "操作参数", "渣铁处理", "炉缸炉体监控", "总分"]
        gaolu_diagnose_records: list = gaolu_diagnose_service.get_records(start, end)
        for record in gaolu_diagnose_records:
            ordered = OrderedDict()
            for index,key in enumerate(keys):
                column = columns[index]
                ordered[column] = record[key]
            results.append(ordered)

        df = pd.DataFrame(data=results)

        return df
